package com.suse.lhw.hms.exersiseserver.controller;

import com.sun.prism.shader.Mask_TextureSuper_AlphaTest_Loader;
import com.suse.lhw.hms.exersiseserver.constants.ConstCollectType;
import com.suse.lhw.hms.exersiseserver.constants.ConstStatuMovementModel;
import com.suse.lhw.hms.exersiseserver.constants.ConstStatusPackage;
import com.suse.lhw.hms.exersiseserver.domin.*;
import com.suse.lhw.hms.exersiseserver.pojo.out.*;
import com.suse.lhw.hms.exersiseserver.service.*;
import com.suse.lhw.hms.exersiseserver.utils.MapperUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @ProjectName: health_manage_system
 * @Package: com.suse.lhw.hms.exersiseserver.com.suse.lhw.security.controller
 * @ClassName: ExersiseUserController
 * @Description: java类作用描述
 * @Author: 李鸿炜
 * @CreateDate: 2020/5/3 14:00
 * @UpdateUser: 更新者
 * @UpdateDate: 2020/5/3 14:00
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
@RestController
@RequestMapping("exersiseUser")
public class ExersiseUserController {
    @Autowired
    private EMoventmodelService eMoventmodelService;
    @Autowired
    private EPackageService ePackageService;
    @Autowired
    private ECollectionService eCollectiosevice;
    @Autowired
    private EUnitService eUnitService;
    @Autowired
    private EPackageInstanceService ePackageInstanceService;
    @Autowired
    private EMmInstanceService eMmInstanceService;

    @RequestMapping("selectAllDefaultModel")
    public List<MovementModelAndUnits> selectAllDefaultModel(int status) {
        /**
         * @Author:Hongwei Li
         * @Description:查找所有的模型
         * @Date:10:49 2020/5/8
         * @Param:[status]
         * @Return:java.util.List<com.suse.lhw.hms.exersiseserver.pojo.out.MovementModelAndUnits>
         **/
        return eMoventmodelService.selectAllModelByStatus(status);
    }

    @RequestMapping(value = "/excuteAMdoel", method = RequestMethod.POST)
    public String excuteAMdoel(@RequestBody String json) {
        /**
         * @Author:Hongwei Li
         * @Description:用户执行一个动作模型
         * @Date:7:34 2020/5/16
         * @Param:[eMoventmodel, eUnit]
         * @Return:java.lang.String
         **/

        MMandEUnit mMandEUnit = null;
        Map<String, Object> map = null;
        try {
            map = MapperUtils.json2map(json);
//            MMandEUnit mMandEUnit1= (MMandEUnit) MapperUtils.json2pojo(json,MMandEUnit.class);
//            System.err.println(mMandEUnit1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        EUnit eUnit = new EUnit(Integer.parseInt(map.get("uid").toString()), map.get("unitName").toString(), Integer.parseInt(map.get("consumCalories").toString()));
        EMoventmodel eMoventmodel = new EMoventmodel(
                Integer.parseInt(map.get("mid").toString()), map.get("mmName").toString(), map.get("mmImgurl").toString(), 1, Integer.parseInt(map.get("status").toString()), 1,
                map.get("mmDescroption").toString(), map.get("userid").toString(),
                map.get("difficulty").toString(), "1", map.get("points").toString()
        );

//        EUnit eUnit=new EUnit(mMandEUnit.getUnitName(),mMandEUnit.getConsumCalories());
        Integer quanlity = Integer.parseInt(map.get("quality").toString());


        String userId = "aaa";
        if (eMoventmodelService.excuteAMM(eUnit, eMoventmodel, quanlity, userId) != 0) {
            return "执行成功";
        } else
            return "执行失败";
    }

    @RequestMapping("selectMoudleById")
    public MovementModelAndUnits selectModelById(int id) {
        /**
         * @Author:Hongwei Li
         * @Description:通过传入的id，查找对应的model
         * @Date:8:28 2020/5/16
         * @Param:[id]
         * @Return:java.util.List<com.suse.lhw.hms.exersiseserver.pojo.out.MovementModelAndUnits>
         **/
        return eMoventmodelService.selectAModelById(id);

    }

    @RequestMapping("selectPackageById")
    public PackageAndModels selectPacakgeById(int id) {
        /**
         * @Author:Hongwei Li
         * @Description:通过传入的id，查找相应的套餐
         * @Date:8:30 2020/5/16
         * @Param:[id]
         * @Return:java.util.List<com.suse.lhw.hms.exersiseserver.pojo.out.PackageAndModels>
         **/
        return ePackageService.selectAPackageById(id);
    }

    @RequestMapping("selectCollectedModel")
    public List<MovementModelAndUnits> selectCollectedModel() {
        /**
         * @Author:Hongwei Li
         * @Description:查找已收藏的单一动作
         * @Date:9:08 2020/5/16
         * @Param:[]
         * @Return:java.util.List<com.suse.lhw.hms.exersiseserver.pojo.out.MovementModelAndUnits>
         **/
        String userId = "aaa";
        return eMoventmodelService.selectCollectedByUseridType(userId, ConstCollectType.model);
    }

    @RequestMapping("selectCollectedPackage")
    public List<PackageAndModels> selectCollectedPackage() {
        /**
         * @Author:Hongwei Li
         * @Description:查找已收藏的动作套餐
         * @Date:9:10 2020/5/16
         * @Param:[]
         * @Return:java.util.List<com.suse.lhw.hms.exersiseserver.pojo.out.PackageAndModels>
         **/
        String userId = "aaa";
        return ePackageService.selectCollectedBYUseridType(userId, ConstCollectType.package1);
    }

    @RequestMapping("collectAModel")
    public String collectAModel(int modelId) {
        /**
         * @Author:Hongwei Li
         * @Description:收藏一个模型
         * @Date:19:28 2020/5/16
         * @Param:[modelId]
         * @Return:java.lang.String
         **/
        String userId = "aaa";
        if (eCollectiosevice.collectByUseridTypeCollectionId(modelId, ConstCollectType.model, userId) == 0)
            return "收藏失败";
        return "收藏成功";
    }

    @RequestMapping("collectAPackage")
    public String collectAPackage(int pacakgeId) {
        /**
         * @Author:Hongwei Li
         * @Description:收藏一个套餐
         * @Date:19:37 2020/5/16
         * @Param:[pacakgeId]
         * @Return:java.lang.String
         **/
        String userId = "aaa";
        if (eCollectiosevice.collectByUseridTypeCollectionId(pacakgeId, ConstCollectType.package1, userId) == 0)
            return "收藏失败！";
        return "收藏成功！";
    }

    @RequestMapping("cancelAModelCollect")
    public String cancelAMdeoCollect(int modelId) {
        /**
         * @Author:Hongwei Li
         * @Description:取消收藏一个模型
         * @Date:19:39 2020/5/16
         * @Param:[modelId]
         * @Return:java.lang.String
         **/
        String userId = "aaa";
        if (eCollectiosevice.cancelByUseridCid(userId, modelId) == 0)
            return "收藏失败！";
        return "收藏成功";
    }

    @RequestMapping("cancelAPackageCollect")
    public String cancelAPackageCollect(int packageId) {
        String userId = "aaa";
        if (eCollectiosevice.cancelByUseridCid(userId, packageId) == 0)
            return "收藏失败！";
        return "收藏成功！！";
    }

    @RequestMapping("selectDIYModels")
    public List<MovementModelAndUnits> selectDIYModel() {
        /**
         * @Author:Hongwei Li
         * @Description:查找用户所有已上传和未上传的自定义动作
         * @Date:9:12 2020/5/16
         * @Param:[]
         * @Return:java.util.List<com.suse.lhw.hms.exersiseserver.pojo.out.MovementModelAndUnits>
         **/
        String userId = "aaa";
        List<MovementModelAndUnits> l1 = eMoventmodelService.selectAllByUseridAndStatusAndName(userId, ConstStatuMovementModel.un_upload, "");
        List<MovementModelAndUnits> l2 = eMoventmodelService.selectAllByUseridAndStatusAndName(userId, ConstStatuMovementModel.uploaded, "");
        l1.addAll(l2);
        return l1;
    }

    @RequestMapping("selectDIYPackages")
    public List<PackageAndModels> selectDIYPackages() {
        /**
         * @Author:Hongwei Li
         * @Description:查找用户所有以上传和未上传的自定义套餐
         * @Date:9:13 2020/5/16
         * @Param:[]
         * @Return:java.util.List<com.suse.lhw.hms.exersiseserver.pojo.out.PackageAndModels>
         **/
        String userId = "aaa";
        List<PackageAndModels> p1 = ePackageService.selectCollectedBYUseridTypeName(userId, ConstStatusPackage.unupload, "");
        List<PackageAndModels> p2 = ePackageService.selectCollectedBYUseridTypeName(userId, ConstStatusPackage.uploaded, "");
        p1.addAll(p2);
        return p1;
    }

    @RequestMapping("uploadAModle")
    public String uploadAModel(int id) {
        /**
         * @Author:Hongwei Li
         * @Description:上传一个未上传动作模型
         * @Date:9:21 2020/5/16
         * @Param:[]
         * @Return:java.lang.String
         **/
        EMoventmodel eMoventmodel = eMoventmodelService.selectByPrimaryKey(id);
        eMoventmodel.setStatus(ConstStatuMovementModel.uploaded);
        if (eMoventmodelService.updateByPrimaryKeySelective(eMoventmodel) == 0)
            return "上传失败!";
        return "上传成功！";
    }

    @RequestMapping("unuploadAMdle")
    public String unuploadAModel(int modelId) {
        /**
         * @Author:Hongwei Li
         * @Description:取消上传一个动作模型
         * @Date:9:22 2020/5/16
         * @Param:[]
         * @Return:java.lang.String
         **/
        EMoventmodel eMoventmodel = eMoventmodelService.selectByPrimaryKey(modelId);
        eMoventmodel.setStatus(ConstStatuMovementModel.un_upload);
        if (eMoventmodelService.updateByPrimaryKeySelective(eMoventmodel) == 0)
            return "取消上传失败！";
        return "取消上传成功！";
    }

    @RequestMapping("uploadAPackage")
    public String uploadAPacakge(int pakcageId) {
        /**
         * @Author:Hongwei Li
         * @Description:上传一个套餐
         * @Date:20:56 2020/5/16
         * @Param:[pakcageId]
         * @Return:java.lang.String
         **/
        EPackage ePackage = ePackageService.selectByPrimaryKey(pakcageId);
        ePackage.setStatus(ConstStatusPackage.uploaded);
        if (ePackageService.updateByPrimaryKeySelective(ePackage) == 0)
            return "上传成功！";
        return "上传失败！";
    }

    @RequestMapping("unuploadAPackage")
    public String unuploadAPackage(int pakcageId) {
        /**
         * @Author:Hongwei Li
         * @Description:取消上传一个套餐
         * @Date:21:01 2020/5/16
         * @Param:[pakcageId]
         * @Return:java.lang.String
         **/
        EPackage ePackage = ePackageService.selectByPrimaryKey(pakcageId);
        ePackage.setStatus(ConstStatusPackage.unupload);
        if (ePackageService.updateByPrimaryKeySelective(ePackage) == 0)
            return "取消掉上传失败";
        return "取消上传成功";
    }

    @RequestMapping("addADIYModel")
    public String addADIYModel(@RequestBody eMoventmodel_units a) {
        /**ccccccccccccccccccccccccccccccccccccccccc
         * @Author:Hongwei Li
         * @Description:增加一个自定义动作
         * @Date:10:40 2020/5/16
         * @Param:[movementModelAndUnits]
         * @Return:java.lang.String
         **/
        System.out.println("a is " + a);
        List<EUnit> units = a.getUnits();
        EMoventmodel eMoventmodel = new EMoventmodel(a.getMmName(), a.getMmImgurl(), a.getMmCategoryId(), a.getStatus(), a.getMmPackagetype(), a.getMmDescription(), a.getUserid(), a.getDifficulty(), a.getVideourl(), a.getPoints());
        if (eMoventmodelService.addAMovenmentModelByStatus(eMoventmodel, units) == 0)
            return "添加自定义失败";
        return "添加成功！";
    }

    @RequestMapping("addADIYPackage")
    public String addADIYPackage(PackageAndModels packageAndModels) {
        /**ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
         * @Author:Hongwei Li
         * @Description:增加一个套餐
         * @Date:10:41 2020/5/16
         * @Param:[packageAndModels]
         * @Return:java.lang.String
         **/

        List<EModelsOfPackage> list = packageAndModels.getList();
        EPackage ePackage = new EPackage(packageAndModels.getImgurl(), packageAndModels.getName(), packageAndModels.getDescription(), packageAndModels.getVideourl(), packageAndModels.getStatus(), packageAndModels.getCategoryid(), packageAndModels.getUserid(), packageAndModels.getDifficulty());
        if (ePackageService.insertAPackageAndModels(ePackage, list) == 0)
            return "添加成功！";
        return "添加失败！";
    }

    @RequestMapping("selectUnitsByModelId")
    public MovementModelAndUnits selectUnitsByModelId(int id) {
        /**
         * @Author:Hongwei Li
         * @Description:通过模型id查找单位
         * @Date:10:49 2020/5/16
         * @Param:[id]
         * @Return:com.suse.lhw.hms.exersiseserver.pojo.out.MovementModelAndUnits
         **/
        return eUnitService.selectByModelId(id);
    }

    @RequestMapping("selectModelsByPackageId")
    public PackageAndModels selectMddelByPackageId(int pacakgeId) {
        /**
         * @Author:Hongwei Li
         * @Description:通过套餐id找模型们
         * @Date:10:53 2020/5/16
         * @Param:[pacakgeId]
         * @Return:com.suse.lhw.hms.exersiseserver.pojo.out.PackageAndModels
         **/
        return ePackageService.selectAPackageById(pacakgeId);
    }


    @PostMapping("excuteAPackage")
    public String excuteAPacakge(EPackage ePackage) {
        /**
         * @Author:Hongwei Li
         * @Description:直接将套餐加入用户id和时间存入
         *
         * @Date:8:19 2020/5/16
         * @Param:[ePackage]
         * @Return:java.lang.String
         **/

        if (ePackageInstanceService.exuteAPackage(ePackage) == 0)
            return "执行失败！";
        return "执行成功！";
    }

    @RequestMapping("selectByNameUseridAndStatus")
    public List<MovementModelAndUnits> selectByNameUseridAndStatus(@RequestParam String name, @RequestParam int status, @RequestParam String userid) {
        System.out.println("name is" + name);
        List<MovementModelAndUnits> list = eMoventmodelService.selectAllByUseridAndStatusAndName(userid, status, name);
        if (list == null)
            return null;
        for (MovementModelAndUnits movementModelAndUnits : list) {
            movementModelAndUnits.toString();
        }
        return list;
    }

    @RequestMapping("selectMovInstance")
    public List<EIAndName> selectMovInstance() {
        String userId = "aaa";
        List<EIAndName> list = eMmInstanceService.selectMovInstance();
        if (list != null)
            for (EIAndName eiAndName : list) {
                System.out.println(eiAndName.toString());
            }
        if (list == null || list.isEmpty())
            return null;
        return list;
    }

    @ResponseBody
    @RequestMapping("deleteMovInstance")
    public int deleteMovInstance(@RequestParam Integer movInstanceId) {
        System.out.println("posted instanceId is" + movInstanceId);
        int result = eMmInstanceService.deleteByPrimaryKey(movInstanceId);
        System.out.println("the jdbc return is " + result);
        return result;
    }

    @ResponseBody
    @RequestMapping("deleteAMovModel")
    public int deleteAMovModel(@RequestParam Integer movId) {
        String result = "";
        int serviceResult = eMoventmodelService.deleteByModelId(movId);
        return serviceResult;
    }
}
